home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_Tix.idb / usr / freeware / lib / tix4.1 / Event.tcl.z / Event.tcl
Encoding:
Text File  |  1999-01-26  |  5.4 KB  |  236 lines

  1. # Event.tcl --
  2. #
  3. #    Handles the event bindings of the -command and -browsecmd options
  4. #    (and various of others such as -validatecmd).
  5. #
  6. # Copyright (c) 1996, Expert Interface Technologies
  7. #
  8. # See the file "license.terms" for information on usage and redistribution
  9. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10. #
  11.  
  12. #----------------------------------------------------------------------
  13. # Evaluate high-level bindings (-command, -browsecmd, etc):
  14. # with % subsitution or without (compatibility mode)
  15. #
  16. #
  17. # BUG : if a -command is intercepted by a hook, the hook must use
  18. #       the same record name as the issuer of the -command. For the time
  19. #    being, you must use the name "bind" as the record name!!!!!
  20. #
  21. #----------------------------------------------------------------------
  22. set _tix_event_flags ""
  23. append _tix_event_flags " %%"
  24. append _tix_event_flags " %#"
  25. #append _tix_event_flags " %a"
  26. append _tix_event_flags " %b"
  27. append _tix_event_flags " %c"
  28. append _tix_event_flags " %d"
  29. append _tix_event_flags " %f"
  30. append _tix_event_flags " %h"
  31. append _tix_event_flags " %k"
  32. append _tix_event_flags " %m"
  33. append _tix_event_flags " %o"
  34. append _tix_event_flags " %p"
  35. append _tix_event_flags " %s"
  36. append _tix_event_flags " %t"
  37. append _tix_event_flags " %w"
  38. append _tix_event_flags " %x"
  39. append _tix_event_flags " %y"
  40. append _tix_event_flags " %A"
  41. append _tix_event_flags " %B"
  42. append _tix_event_flags " %E"
  43. append _tix_event_flags " %K"
  44. append _tix_event_flags " %N"
  45. append _tix_event_flags " %R"
  46. #append _tix_event_flags " %S"
  47. append _tix_event_flags " %T"
  48. append _tix_event_flags " %W"
  49. append _tix_event_flags " %X"
  50. append _tix_event_flags " %Y"
  51.  
  52. proc tixBind {tag event action} {
  53.     global _tix_event_flags
  54.  
  55.     append cmd "_tixRecordFlags $event $_tix_event_flags;"
  56.     append cmd "$action; "
  57.     append cmd "_tixDeleteFlags"
  58.  
  59.     bind $tag $event $cmd
  60. }
  61.  
  62. # This is a "name stack" for storing the "bind" structures
  63. #
  64. # The bottom of the event stack is usually a raw event (generated by tixBind)
  65. # but it may also be a programatically triggered (caused by tixEvalCmdBinding)
  66. #
  67. #
  68.  
  69. set tixEvent(nameStack)        ""
  70. set tixEvent(stackLevel)        0
  71.  
  72. proc tixPushEventStack {} {
  73.     global tixEvent
  74.  
  75.     set lastEvent [lindex $tixEvent(nameStack) 0]
  76.     incr tixEvent(stackLevel)
  77.     set thisEvent _tix_event$tixEvent(stackLevel)
  78.  
  79.     set tixEvent(nameStack) \
  80.     [list $thisEvent $tixEvent(nameStack)]
  81.  
  82.     if {$lastEvent == ""} {
  83.     upvar #0 $thisEvent this
  84.     set this(type) <Application>
  85.     } else {
  86.     upvar #0 $lastEvent last
  87.     upvar #0 $thisEvent this
  88.  
  89.     foreach name [array names last] {
  90.         set this($name) $last($name)
  91.     }
  92.     }
  93.  
  94.     return $thisEvent
  95. }
  96.  
  97. proc tixPopEventStack {varName} {
  98.     global tixEvent
  99.  
  100.     if {$varName != [lindex $tixEvent(nameStack) 0]} {
  101.     error "unmatched tixPushEventStack and tixPopEventStack calls"
  102.     }
  103.     incr tixEvent(stackLevel) -1
  104.     set tixEvent(nameStack) [lindex $tixEvent(nameStack) 1]
  105.     global $varName
  106.     unset $varName
  107. }
  108.  
  109.  
  110. # Events triggered by tixBind
  111. #
  112. proc _tixRecordFlags [concat event $_tix_event_flags] {
  113.     global _tix_event_flags
  114.  
  115.     set thisName [tixPushEventStack]; upvar #0 $thisName this
  116.  
  117.     set this(type) $event
  118.     foreach f $_tix_event_flags {
  119.     set this($f) [set $f]
  120.     }
  121. }
  122.  
  123. proc _tixDeleteFlags {} {
  124.     global tixEvent
  125.  
  126.     tixPopEventStack [lindex $tixEvent(nameStack) 0]
  127. }
  128.  
  129. # programatically trigged events
  130. #
  131. proc tixEvalCmdBinding {w cmd {subst ""} args} {
  132.     global tixPriv tixEvent tix
  133.  
  134.     set thisName [tixPushEventStack]; upvar #0 $thisName this
  135.  
  136.     if {$subst != ""} {
  137.     upvar $subst bind
  138.  
  139.     if [info exists bind(specs)] {
  140.         foreach spec $bind(specs) {
  141.         set this($spec) $bind($spec)
  142.         }
  143.     }
  144.     if [info exists bind(type)] {
  145.         set this(type) $bind(type)
  146.     }
  147.     }
  148.  
  149.     if [catch {
  150.     if [tixGetBoolean -nocomplain $tix(-extracmdargs)] {
  151.         # Compatibility mode
  152.         #
  153.         set ret [uplevel #0 $cmd $args]
  154.     } else {
  155.         set ret [uplevel $cmd]
  156.     }
  157.     } error] {
  158.     if [catch {
  159.         tixCmdErrorHandler $error
  160.     } error] {
  161.         # double fault: just print out 
  162.         tixBuiltInCmdErrorHandler $error
  163.     }
  164.     tixPopEventStack $thisName
  165.     return ""
  166.     } else {
  167.     tixPopEventStack $thisName
  168.  
  169.     return $ret
  170.     }
  171. }
  172.  
  173. proc tixEvent {option args} {
  174.     global tixPriv  tixEvent
  175.     set varName [lindex $tixEvent(nameStack) 0]
  176.  
  177.     if {$varName == ""} {
  178.     error "tixEvent called when no event is being processed"
  179.     } else {
  180.     upvar #0 $varName event
  181.     }
  182.  
  183.     case $option {
  184.     type {
  185.         return $event(type)
  186.     }
  187.     value {
  188.         if [info exists event(%V)] {
  189.         return $event(%V)
  190.         } else {
  191.         return ""
  192.         }
  193.     }
  194.     flag {
  195.         set f %[lindex $args 0]
  196.         if [info exists event($f)] {
  197.         return $event($f)
  198.         }
  199.         error "The flag \"[lindex $args 0]\" does not exist"
  200.     }
  201.     match {
  202.         return [string match [lindex $args 0] $event(type)]
  203.     }
  204.     default {
  205.         error "unknown option \"$option\""
  206.     }
  207.     }
  208. }
  209.  
  210. # tixBuiltInCmdErrorHandler --
  211. #
  212. #    Default method to report command handler errors. This procedure is
  213. #    also called if double-fault happens (command handler causes error,
  214. #    then tixCmdErrorHandler causes error).
  215. #
  216. proc tixBuiltInCmdErrorHandler {errorMsg} {
  217.     global errorInfo
  218.     if ![info exists errorInfo] {
  219.     set errorInfo "???"
  220.     }
  221.     puts "Error:\n $errorMsg\n$errorInfo"
  222. }
  223.  
  224. # tixCmdErrorHandler --
  225. #
  226. #    You can redefine this command to handle the errors that occur
  227. #    in the command handlers. See the programmer's documentation
  228. #    for details
  229. #
  230. if ![string compare [info command tixCmdErrorHandler] ""] {
  231.     proc tixCmdErrorHandler {errorMsg} {
  232.     tixBuiltInCmdErrorHandler $errorMsg
  233.     }
  234. }
  235.  
  236.